package cn.wind.xboot.core.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import org.springframework.util.StringUtils;

import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;


/**
 *
 * @author xukk
 * @date 2018/5/4
 */
public class AESUtil {
    public static final String CONSTANT_CBC_ALGORITHM="AES/CBC/PKCS5Padding";
    public static final String CONSTANT_ECB_ALGORITHM="AES/ECB/PKCS5Padding";
    private static final String CONSTANT_KEY="y2W89L6BkRAFljhN";
    private static final String CONSTANT_IV="dMitHORyqbeYVE0o";
    private static final Integer INIT_NUM = 128;
    public static AESUtil CBC =new AESUtil();
    public static AESUtil ECB=new AESUtil();
    static {
        CBC.aes=SecureUtil.aes();
        CBC.aes.setIv(CONSTANT_IV.getBytes());
        CBC.aes.init(CONSTANT_CBC_ALGORITHM,new SecretKeySpec(CONSTANT_KEY.getBytes(), SymmetricAlgorithm.AES.getValue()));
        ECB.aes=SecureUtil.aes();
        ECB.aes.init(CONSTANT_ECB_ALGORITHM,new SecretKeySpec(CONSTANT_KEY.getBytes(), SymmetricAlgorithm.AES.getValue()));
    }

    private String algorithm;

    private  AES aes;

    private  String key;

    private  String iv;

    public static AESUtil builder(){
        AESUtil aesUtil=new AESUtil();
        return aesUtil;
    }
    public AESUtil build(){
        this.aes=SecureUtil.aes();
        if(StringUtils.hasText(iv)){
            this.aes.setIv(iv.getBytes());
        }
        if(StringUtils.hasText(algorithm)){
            this.aes.init(algorithm,new SecretKeySpec(key.getBytes(), SymmetricAlgorithm.AES.getValue()));
        }
        return this;
    }
    public String encrypt(String data) {
        return aes.encryptBase64(data.getBytes());
    }
    public String decrypt(String encodeData){
        return aes.decryptStrFromBase64(encodeData);
    }

    public AESUtil iv(String iv){
        this.iv=iv;
        return this;
    }
    public AESUtil key(String key){
        this.key=key;
        return this;
    }
    public AESUtil algorithm(String algorithm){
        this.algorithm=algorithm;
        return this;
    }
    public static void main(String[] args) {
        System.err.println("公钥加密——私钥解密");
       // String data = "{'tax_territoriality': '浙江杭州', 'tax_company': '浙江瑞登资产管理有限公司','tax_invoice_type': '增值税普通发票','tax_payer_type': '小规模','tax_payer_code': '91330106341878268F','tax_rate': '3%'}";
         String data="Basic YmFjazpiYWNrVXNlcg==";
        String encodeStr=AESUtil.CBC.encrypt(data);
        System.out.println("\r加密后文字: \r\n"+encodeStr);
        String decodeStr=AESUtil.CBC.decrypt(encodeStr);
        System.out.println("\r解密后文字: \r\n"+decodeStr);
        test();
    }
    public static void test(){
        String a= "pageNumber=1&pageSize=2";
        String c=AESUtil.ECB.encrypt(a);
        System.out.println(c);
        String d= Base64.getUrlEncoder().encodeToString(c.getBytes());
        System.out.println(d);
        System.out.println(AESUtil.ECB.decrypt(new String(Base64.getUrlDecoder().decode(d.getBytes()))));
    }
}
